package com.qu.Media.media.adpater;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;

import com.bumptech.glide.Glide;
import com.qu.Media.R;
import com.qu.Media.media.bean.Image;
import com.qu.Media.media.config.ImageLoaderListener;

import java.util.ArrayList;
import java.util.List;

/**
 * 图片的适配器
 * Created by quguangle on 2017/4/18.
 */

public class ImageAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder>{
    private List<Image> datas;
    private Context mContext;
    private final LayoutInflater inflater;
    private ImageLoaderListener loader;
    private boolean isSingleSelect;
    public ImageAdapter(Context mContext,ImageLoaderListener loader){
        this.mContext = mContext;
         this.loader = loader;
          inflater = LayoutInflater.from(mContext);
        if(datas == null){
            datas = new ArrayList<>();
        }
    }
    public void setSingleSelect(boolean singleSelect) {
        isSingleSelect = singleSelect;
    }

    @Override
    public int getItemViewType(int position) {
        Image image = getItem(position);
        if (image.getId() == 0)
            return 0;
        return 1;
    }
    @Override
    public void onViewRecycled(RecyclerView.ViewHolder holder) {
        if (holder instanceof ImageViewHolder) {
            ImageViewHolder h = (ImageViewHolder) holder;
            Glide.clear(h.mImageView);
        }
    }
    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        if (viewType == 0)
            return new CamViewHolder(inflater.inflate(R.layout.item_list_cam, parent, false),mContext);
        return new ImageViewHolder(inflater.inflate(R.layout.item_list_image, parent, false),mContext);
    }

    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
        if(holder instanceof ImageViewHolder) {
            ImageViewHolder imageViewHolder = (ImageViewHolder) holder;
            imageViewHolder.setData(position, datas.get(position));
        }
    }

    @Override
    public int getItemCount() {
        return datas.size();
    }
    //没有照相机ViewHolder
    class ImageViewHolder extends RecyclerView.ViewHolder{
        private Context mContext;
        public ImageView mImageView;
        public ImageView mCheckView;
        public ImageView mGifMask;
        public View mMaskView;
        public ImageViewHolder(View itemView,Context mContext) {
            super(itemView);
            this.mContext = mContext;
            mImageView = (ImageView) itemView.findViewById(R.id.iv_image);
            mCheckView = (ImageView) itemView.findViewById(R.id.cb_selected);
            mMaskView = itemView.findViewById(R.id.lay_mask);
            mGifMask = (ImageView) itemView.findViewById(R.id.iv_is_gif);
            itemView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    if(onImageItemClick != null){
                        onImageItemClick.onItemClick(getLayoutPosition());
                    }
                }
            });
        }
        public void setData(int position,Image item){
            if (item.getId() != 0) {
                mCheckView.setSelected(item.isSelect());
                mMaskView.setVisibility(item.isSelect() ? View.VISIBLE : View.GONE);

                // Show gif mask
                mGifMask.setVisibility(item.getPath().toLowerCase().endsWith("gif") ?
                        View.VISIBLE : View.GONE);
                //显示图片
                loader.displayImage(mImageView, item.getPath());
                mCheckView.setVisibility(isSingleSelect ? View.GONE : View.VISIBLE);
            }
        }
    }
     //只有照相机ViewHolder
    class CamViewHolder extends RecyclerView.ViewHolder{
        private Context mContext;
        public CamViewHolder(View itemView,Context mContext) {
            super(itemView);
            this.mContext = mContext;
            itemView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    if(onImageItemClick != null){
                        onImageItemClick.onItemClick(getLayoutPosition());
                    }
                }
            });
        }
    }

    public Image getItem(int position){
        if(position >=0 && position< datas.size()){
            return datas.get(position);
        }
        return null;
    }
    public void updateItem(int positon){
        if(datas.size() > positon){
             notifyItemChanged(positon);
        }
    }
    public final void addItem(Image item) {
        if (item != null) {
            this.datas.add(item);
            notifyItemChanged(datas.size());
        }
    }
    public void addAll(List<Image> items) {
        if (items != null) {
            this.datas.addAll(items);
            notifyItemRangeInserted(this.datas.size(), items.size());
        }
    }
    public final void clear() {
        this.datas.clear();
        notifyDataSetChanged();
    }
    private OnImageItemClick onImageItemClick;
    public interface OnImageItemClick{
        void onItemClick(int position);
    }
    public void setOnImageItemClick(OnImageItemClick onImageItemClick){
        this.onImageItemClick = onImageItemClick;
    }
}
